import axios from "axios"
import { ElMessage } from 'element-plus';
import useUserStore from '@/store/module/user';

const request = axios.create({
    baseURL: "/api",
    timeout: 5000
})

request.interceptors.request.use((config) => {
    let userStore = useUserStore();
    if (userStore.userInfo.token) {
        config.headers.token = userStore.userInfo.token;
    }
    return config
})

request.interceptors.response.use((res) => {

    return res.data

}, (err) => {
    switch (err.response?.status) {
        case 404:
            ElMessage({
                type: 'error',
                message: '请求失败路径出现问题'
            })
            break;
        case 500 || 501 || 502 || 503 || 504 || 505:
            ElMessage({
                type: 'error',
                message: '服务器挂了'
            })
            break;
        case 401:
            ElMessage({
                type: 'error',
                message: '参数有误'
            })
            break;
    };
    return Promise.reject(new Error(err.message))
})

export default request

